<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <div>
      <span>商品</span>
      <product @addCart="addCart"></product>
    </div>
    <hr>
    <div>
      <span>购物车</span>
      <cart :products="products"></cart>
    </div>
    <hr>
    <br>
    <vuex-btn></vuex-btn>
  </div>
</template>

<script>
import product from './components/product'
import cart from './components/cart'
import vuexBtn from './components/vuexBtn'
export default {
  name: 'App',
  components: {
    product,
    cart,
    vuexBtn
  },
  data () {
    return {
      products: []
    }
  },
  methods: {
    // 接收product组件传值 val
    addCart(val) {
      let index = this.products.findIndex(item => {
        return item.id === val.id
      })
      if(index < 0) {
        // 此处需要给额外添加的number属性 添加响应式
        this.$set(val, "number", 1)
        this.products.push(val)
      }else {
        this.products[index].number++
      }
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
